package ftdpc.centre;

import ftdpc.application.IDispatchTaskController;
import ftdpc.bean.TaskAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

/**
 * 类描述: 调度任务控制器管理接口注册器
 *
 * @author : 王雷
 * @date : 2022/9/14 0014 下午 1:55
 */
@Slf4j
//@Component
public class DispatchTaskControllerConfig {
    /**
     * 属性描述：调度任务控制器处理交互前缀前缀
     *
     * @date : 2022/9/14 0014 下午 1:55
     */
    private static final String REDIS_CACHE_KEY_PREFIX = "/dispatch/task/manager/";

    protected DispatchTaskControllerConfig(RequestMappingHandlerMapping requestMappingHandlerMapping
            , IDispatchTaskController iDispatchTaskController) {
        log.info("开始加载调度任务控制器管理接口");
        //获取所有当前服务缓存描述
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "giveTaskAttributeList", "giveTaskAttributeList");
        //根据标识获取任务描述属性
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "giveTaskAttributeItem", "giveTaskAttributeItem", String.class);
        //获取任务日志信息
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "giveTaskLog", "giveTaskLog", String.class);
        //线程启动或关闭
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "taskStartOrStop", "taskStartOrStop", String.class);
        //将属性存入配置
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "putToConfig", "putToConfig", String.class);
        //修改配置属性
        registerInteractionObject(requestMappingHandlerMapping, iDispatchTaskController
                , REDIS_CACHE_KEY_PREFIX + "updateAttribute", "updateAttribute", TaskAttribute.class);
    }

    private void registerInteractionObject(RequestMappingHandlerMapping requestMappingHandlerMapping
            , IDispatchTaskController redisManagerController, String path, String methodName, Class<?>... parameterTypes) {

        try {
            //注册Mapping信息逻辑
            requestMappingHandlerMapping.registerMapping(RequestMappingInfo.paths(path).build(), redisManagerController
                    , redisManagerController.getClass().getDeclaredMethod(methodName, parameterTypes));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        final Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
        log.debug("map：{}", handlerMethods);
        handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
            log.debug("requestMappingInfo：{}", requestMappingInfo);
            log.debug("handlerMethod：{}", handlerMethod);
        });
    }
}
